package com.security.filter.conf;

import com.security.filter.domain.SecurityDO;
import com.security.filter.service.SecurityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import java.util.ArrayList;

/**
 * @Classname MyUserDetailsService
 * @Description TODO
 * @Date 2020/4/21 0021 12:27
 * @Created by 埔枘
 */
@Service
public class MyUserDetailsService implements UserDetailsService {

    @Autowired
    private SecurityService securityService;

//
//    MyUserDetailsService(SecurityService securityService){
//        this.securityService = securityService;
//    }

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        SecurityDO securityDO = securityService.loginByUsername(username);
        if(null == securityDO){
            throw new UsernameNotFoundException("用户不存在");
        }
        ArrayList<SimpleGrantedAuthority> simpleGrantedAuthorities = new ArrayList<>();
        SimpleGrantedAuthority simpleGrantedAuthority = new SimpleGrantedAuthority("USER");
        simpleGrantedAuthorities.add(simpleGrantedAuthority);
        return new User(securityDO.getUsername(),securityDO.getPassword(),simpleGrantedAuthorities);
    }
}
